using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Zoo;
using System.ComponentModel;

public partial class _Default : PageBase, IDefaultView
{
    private DefaultController controller;

    #region properties

    public System.Security.Principal.IIdentity UserIdentity
    {
        get
        {
            return User.Identity;
        }
    }

    public string Password
    {
        get
        {
            return PasswordTextBox.Text;
        }
        set
        {
            PasswordTextBox.Text = value;
        }
    }

    public bool LoginBoxVisible
    {
        get
        {
            return LoginBox.Visible;
        }
        set
        {
            LoginBox.Visible = value;
        }
    }

    public string LoginName
    {
        get
        {
            return LoginTextBox.Text;
        }
        set
        {
            LoginTextBox.Text = value;
        }
    }

    public int MissedLoginAttempts
    {
        get
        {
            return Convert.ToInt32(Session["MissedLoginAttempt"]);
        }
        set
        {
            Session["MissedLoginAttempt"] = value;
        }
    }

    public bool InvalidLoginMessageVisible
    {
        get
        {
            return ErrorMessageInvalidLoginLabel.Visible;
        }
        set
        {
            ErrorMessageInvalidLoginLabel.Visible = value;
        }
    }

    public bool Max3AuthenticatedMessageVisible
    {
        get
        {
            return Max3LoginAttempts.Visible;
        }
        set
        {
            Max3LoginAttempts.Visible = value;
        }
    }

    public bool AuthenticatedBoxVisible
    {
        get
        {
            return AuthenticatedBox.Visible;
        }
        set
        {
            AuthenticatedBox.Visible = value;
        }
    }
    
    #endregion

    #region eventsDeclaration
    public event EventHandler PressLogin;

    protected virtual void OnPressLogin(object sender, EventArgs e)
    {
        if (PressLogin != null)
            PressLogin(this, e);
    }
    #endregion
    
    #region methods 
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        controller = new DefaultController(this, model);
    }

    public void SetAuthentication(int ZooUserID)
    {
        FormsAuthentication.SetAuthCookie(ZooUserID.ToString(), false);
        HttpCookie loginNameCookie = new HttpCookie("LoginName", LoginName);
        loginNameCookie.Expires = DateTime.Now.AddMonths(12);
        Response.Cookies.Add(loginNameCookie);
        Response.Redirect(Request.Path);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        controller.Initialize();
        
        Page.DataBind();
    }

    protected void LoginButton_Click(object sender, EventArgs e)
    {
        OnPressLogin(sender, e);
    }
   #endregion


    #region IDefaultView Members


    public string CookieLoginName
    {
        get
        {
            if (Request.Cookies["LoginName"] != null)
                return Request.Cookies["LoginName"].Value;

            return string.Empty;
        }
    }


    public Zoo.Domain.IdentityCard[] NewUsers
    {
        get
        {

            return (Zoo.Domain.IdentityCard[])RepNewProfiles.DataSource; ;
        }
        set
        {
            RepNewProfiles.DataSource = value;
        }
    }

    #endregion

   
}
